﻿
using AdminManager.Permissions;
using AdminManager.System.NavigationsMenu;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Json;

namespace AdminManager.System
{
    [Authorize(AdminManagerPermissions.Navigations.Default)]
    public class NavigationsAppService : ApplicationService, INavigationsAppService
    {
        private readonly IRepository<Navigations> _navigationRepository;
        private readonly IRepository<RoleNavigation> _roleNavigationRepository;
        private readonly IdentityRoleManager _identityRoleManager; 
        public NavigationsAppService(IRepository<Navigations> navigationRepository, 
            IRepository<RoleNavigation> roleNavigationRepository,
            IdentityRoleManager identityRoleManager)
        {
            this._navigationRepository = navigationRepository;
            this._roleNavigationRepository = roleNavigationRepository;
            this._identityRoleManager = identityRoleManager;
        }
        [Authorize(AdminManagerPermissions.Navigations.Create)]

        public async Task<NavigationInputDto> CreateAsync(NavigationInputDto inputDto)
        {
            Navigations item =  ObjectMapper.Map<NavigationInputDto,Navigations>(inputDto);
            await this._navigationRepository.InsertAsync(item);
            inputDto.Id = item.Id;
            return inputDto;
        }

        public async Task<List<NavigationsDto>> GetAllAsync(NavigationsSearchDto searchDto)
        { 

            List<Navigations> list =  (await this._navigationRepository.GetQueryableAsync()).AsNoTracking()
                .WhereIf(!string.IsNullOrWhiteSpace(searchDto.name),p=>p.Name.Contains(searchDto.name)).ToList();
            return ObjectMapper.Map<List<Navigations>, List<NavigationsDto>>(list);
        }

        public async Task<NavigationsDto> GetAsync(string id)
        {
            Navigations item = (await this._navigationRepository.GetQueryableAsync()).AsNoTracking().First(p=>p.Id==Guid.Parse(id));
            if (item == null)
            {
                throw new BusinessException(AdminManagerDomainErrorCodes.NotFoundData);
            }
            return ObjectMapper.Map<Navigations, NavigationsDto>(item);
        }
        

        [Authorize(AdminManagerPermissions.Navigations.Update)]
        public async Task<NavigationsDto> UpdateAsync(string id,NavigationsDto inputDto)
        {
            var item = await _navigationRepository.FirstOrDefaultAsync(p => p.Id == Guid.Parse(id));
            if (item == null)
            {
                throw new BusinessException(AdminManagerDomainErrorCodes.NotFoundData);
            }
            //update 
            item.ParentId = inputDto.ParentId;
            item.Name = inputDto.Name;
            item.Sort = inputDto.Sort;
            item.RouterPath = inputDto.RouterPath;
            item.ComponentPath = inputDto.ComponentPath;
            item.IsState = inputDto.IsState;
            //item.IsKeepalive = inputDto.IsKeepalive;
            //item.IsUrl = inputDto.IsUrl;
            item.Type = inputDto.Type;
            item.Icon = inputDto.Icon;
            item.Actions = inputDto.Actions;
            item.Redirect = inputDto.Redirect;
            await this._navigationRepository.UpdateAsync(item);
            return inputDto;
        }
    }
}
